home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 October: Mac OS SDK / Dev.CD Oct 97 SDK1.toast / Development Kits (Disc 1) / Interfaces&Libraries / OpenTransport / Interfaces / CIncludes / OpenTptModule.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-21  |  28.7 KB  |  1,010 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        OpenTptModule.h
  3.  
  4.     Contains:    headers for STREAM modules developement
  5.  
  6.     Copyright:    © 1993-1996 by Apple Computer, Inc. and Mentat Inc., all rights reserved.
  7.  
  8. */
  9.  
  10. #ifndef __OPENTPTMODULE__
  11. #define __OPENTPTMODULE__
  12.  
  13. #define OTKERNEL    1
  14.  
  15. #ifndef __OPENTPTCOMMON__
  16. #include <OpenTptCommon.h>
  17. #endif
  18. /*
  19.  * Some typedefs needed to include cred.h and mistream.h
  20.  */
  21. typedef UInt32    uid_t;
  22. typedef UInt32    gid_t;
  23. typedef UInt32    dev_t;
  24.  
  25. #ifndef __CRED__
  26. #include <cred.h>
  27. #endif
  28. #ifndef __MISTREAM__
  29. #include <mistream.h>
  30. #endif
  31. #ifndef __STROPTS__
  32. #include <stropts.h>
  33. #endif
  34. #ifndef __STRLOG__
  35. #include <strlog.h>
  36. #endif
  37.  
  38. #ifndef __MEMORY__
  39. #include <Memory.h>
  40. #endif
  41.  
  42. #if defined(__MWERKS__) && GENERATING68K
  43. #pragma pointers_in_D0
  44. #endif
  45.  
  46. #if PRAGMA_ALIGN_SUPPORTED
  47. #pragma options align=mac68k
  48. #endif
  49.  
  50. /*******************************************************************************
  51. ** Printing functions
  52. ********************************************************************************/
  53.  
  54. enum
  55. {
  56.     kOTPrintOnly        = 0,
  57.     kOTPrintThenStop    = 1
  58. };
  59.  
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63.  
  64. int OTKernelPrintf(int toDo, char* fmt, ...);
  65.  
  66. #define CE_CONT        0            /* Does kOTPrintOnly        */
  67. #define CE_WARN        1            /* Does kOTPrintThenStop    */
  68. #define    CE_PANIC    2            /* Does System Error 107    */
  69.  
  70. void cmn_err(int type, char* fmt, ...);
  71.  
  72. #ifdef __cplusplus
  73. }
  74. #endif
  75.  
  76. /*******************************************************************************
  77. ** Function to convert the "long" value that comes back in some of the
  78. ** netbufs as a result code to the equivalent OSStatus
  79. ********************************************************************************/
  80.  
  81. typedef long                    OTError;
  82.  
  83. #define GetEError(v)            ((OTUnixErr)(((v) >> 16) & 0xffff))
  84. #define GetXTIError(v)            ((OTXTIErr)((v) & 0xffff))
  85. #define MakeTPIEError(e)        ((OTError)(((((UInt16)(e)) << 16) | TSYSERR)))
  86. #define MakeDLPIEError(e)        ((OTError)(((((UInt16)(e)) << 16) | DL_SYSERR)))
  87. #define MakeXTIError(xti)        ((OTError)(xti))
  88. #define MakeOTError(xti, e)        ((OTError)((xti) | ((UInt16)(e)) << 16))
  89.  
  90. #ifdef __cplusplus
  91. extern "C" {
  92. #endif
  93.  
  94. OSStatus OTErrorToOSStatus(OTError);
  95.                     
  96. #ifdef __cplusplus
  97. }
  98. #endif
  99.  
  100. /*******************************************************************************
  101. ** Things need to be able to export a module
  102. ********************************************************************************/
  103.  
  104. /*    -------------------------------------------------------------------------
  105.     Synchronization level codes.  These are supplied to modsw_install and
  106.     stored in the appropriate tables.  sth_osr_open and
  107.     sth_ipush use these to set up synch queue subordination for new devices
  108.     and modules.
  109.     ------------------------------------------------------------------------- */
  110.  
  111.     enum
  112.     {
  113.         SQLVL_QUEUE            = 1,
  114.         SQLVL_QUEUEPAIR        = 2,
  115.         SQLVL_MODULE        = 3,
  116.         SQLVL_GLOBAL        = 4,
  117.     
  118.         SQLVL_DEFAULT        = SQLVL_MODULE
  119.     };
  120.  
  121. /*    -------------------------------------------------------------------------
  122.     The install_info structure.
  123.     ------------------------------------------------------------------------- */
  124.  
  125.     struct install_info
  126.     {
  127.         struct streamtab*    install_str;     /* Streamtab pointer.        */
  128.         UInt32                 install_flags;
  129.         UInt32                 install_sqlvl;    /* Synchronization level.    */
  130.         char*                install_buddy;    /* Shared writer list buddy */
  131.         long                ref_load;        /* Set to 0                    */
  132.         UInt32                ref_count;        /* set to 0                    */
  133.     };
  134.     
  135.     typedef struct install_info    install_info;
  136.     
  137.     //
  138.     //    Flags used in the install_flags field
  139.     //
  140.     enum
  141.     {
  142.          kOTModIsDriver            = 0x00000001,
  143.          kOTModIsModule            = 0x00000002,
  144.          
  145.          kOTModUpperIsTPI        = 0x00001000,
  146.          kOTModUpperIsDLPI        = 0x00002000,
  147.          kOTModLowerIsTPI        = 0x00004000,
  148.          kOTModLowerIsDLPI        = 0x00008000,
  149.         //
  150.         // This flag says you don't want per-context globals
  151.         //
  152.         kOTModGlobalContext        = 0x00800000,
  153.         //
  154.         // This flag is only valid if kOTModIsDriver is set
  155.         // and the driver is a PCI-card driver using the Name Registry
  156.         //
  157.         kOTModUsesInterrupts    = 0x08000000,
  158.         //
  159.         // This flag is only valid if kOTModIsDriver is set.
  160.         //
  161.         kOTModIsComplexDriver    = 0x20000000,
  162.         //
  163.         // These flags are only valid if kOTModIsModule is set.
  164.         //
  165.         kOTModIsFilter            = 0x40000000
  166.     };
  167.  
  168. #ifdef __cplusplus
  169. extern "C" {
  170. #endif
  171.     
  172. /*    -------------------------------------------------------------------------
  173.     Typedef for the GetOTInstallInfo function
  174.     
  175.     Your module must export this function, and return a pointer to the
  176.     install_info structure for the module.
  177.     ------------------------------------------------------------------------- */
  178.  
  179.     typedef install_info* (*GetOTInstallInfoProcPtr)(void);
  180.  
  181. /*    -------------------------------------------------------------------------
  182.     Typedef for the InitStreamModule function
  183.     
  184.     Your module can optionally export this function.  It will be called 
  185.     whenever your module is about to be loaded into a stream for the
  186.     first time, or if it is about to be reloaded after having been 
  187.     unloaded. Return false if your module should NOT be loaded.
  188.     For STREAMS modules, the void* parameter will be NULL.  For drivers, it
  189.     will be the same cookie parameter that was used for registering the module.
  190.     For PCI card drivers, this will be a pointer to the OTPCIInfo structure,
  191.     which can also be interpreted as a RegEntryIDPtr.
  192.     ------------------------------------------------------------------------- */
  193.     
  194.     typedef Boolean (*InitStreamModuleProcPtr)(void*);
  195.  
  196. /*    -------------------------------------------------------------------------
  197.     Typedef for the TerminateStreamModule function
  198.     
  199.     Your module can optionally export this function.  It will be called 
  200.     whenever your module has been closed for the last time (i.e. no other 
  201.     outstanding instances of the module exist).
  202.     ------------------------------------------------------------------------- */
  203.     
  204.     typedef void (*TerminateStreamModuleProcPtr)(void*);
  205.  
  206. #ifdef __cplusplus
  207. }
  208. #endif
  209.  
  210. /*******************************************************************************
  211. ** Equates for shared library prefixes
  212. ********************************************************************************/
  213.  
  214.     /*
  215.      * Prefixes for Open Transport driver experts.
  216.      */
  217.     #define kOTPortScannerPrefix        "ot:pScnr$"
  218.     
  219.     /*
  220.      * Interface ID for STREAMS Modules for ASLM
  221.      */
  222.     #define kOTModuleInterfaceID        kOTModulePrefix "StrmMod"
  223.  
  224. /*******************************************************************************
  225. ** Functions for dealing with major and minor device numbers
  226. ********************************************************************************/
  227.  
  228.     typedef UInt16            major_t;
  229.     typedef UInt16            minor_t;
  230.     
  231.     /* major part of a device */
  232.     #define getmajor(x)         ((major_t)(((unsigned long)(x)>>16)&0xffff))
  233.     
  234.     /* minor part of a device */
  235.     #define getminor(x)         ((minor_t)((x)&0xffff))
  236.     
  237.     /* make a device number */
  238.     #define makedev(x,y)        ((dev_t)((((dev_t)(x))<<16) | ((y) & 0xffff)))
  239.     
  240.     #define getemajor            getmajor
  241.     #define geteminor            getminor
  242.     #define makedevice            makedev
  243.     
  244.     #define    etoimajor(majnum)    (majnum)
  245.     #define    itoemajor(majnum,j)    (majnum)
  246.  
  247. /*******************************************************************************
  248. ** Some definitions
  249. ********************************************************************************/
  250.  
  251.     /*
  252.      * The first minor number that you should use for CLONEOPENs.
  253.      * Minor numbers 0 through 9 are reserved for use by the modules
  254.      * for various control streams.
  255.      */
  256.      enum
  257.      {
  258.          kFirstMinorNumber    = 10
  259.     };
  260.  
  261.     /*
  262.      * Extra OTCommand codes that may appear on your module queue.
  263.      * These are extensions to the TPI specification for Open Transport.
  264.      * T_PRIVATE_REQ is the first available TPI message number for private
  265.      * use by modules (assuming you don't want to be confused by standard
  266.      * TPI messages).
  267.      */
  268.     enum
  269.     {
  270.         T_TIMER_REQ        = 80,    // Timer event
  271.         T_MIB_REQ        = 81,    // Request module's MIB
  272.         T_MIB_ACK        = 82,    // The module's MIB is available
  273.         
  274.         T_PRIVATE_REQ    = 90    // The first private request available
  275.     };
  276.  
  277. /*******************************************************************************
  278. ** Logging Macros
  279. ********************************************************************************/
  280.  
  281. #ifdef __cplusplus
  282. extern "C" {
  283. #endif
  284.  
  285. void            OTStrlog(queue_t*, int, int, const char*);
  286.  
  287. #ifdef __cplusplus
  288. }
  289. #endif
  290. //
  291. // These are enums for the level value
  292. //
  293. enum
  294. {
  295.     kOTLvlFatal                = 0,
  296.     kOTLvlNonfatal            = 1,
  297.     kOTLvlExtFatal            = 2,
  298.     kOTLvlExtNonfatal        = 3,
  299.     kOTLvlUserErr            = 4,
  300.     kOTLvlInfoErr            = 5,
  301.     kOTLvlInfoOnly            = 6
  302. };
  303.  
  304. #define STRLOG(q, lvl, flags, str)        OTStrlog(q, lvl, flags, str)
  305.  
  306. #if qDebug
  307.     #define STRLOG1(q, lvl, flags, str)    OTStrlog(q, lvl, flags, str)
  308. #else
  309.     #define STRLOG1(q, lvl, flags, str)
  310. #endif
  311.  
  312. #if qDebug > 1 || qDebug2 > 1
  313.     #define STRLOG2(q, lvl, flags, str)    OTStrlog(q, lvl, flags, str)
  314. #else
  315.     #define STRLOG2(q, lvl, flags, str)
  316. #endif
  317.  
  318. /*******************************************************************************
  319. ** Structure definitions
  320. **
  321. ** These go along with the extra definitions above.
  322. ********************************************************************************/
  323.  
  324.     struct  T_MIB_req
  325.     {
  326.         long    PRIM_type;        /* Always T_MIB_REQ */
  327.     };
  328.     
  329.     struct  T_MIB_ack
  330.     {
  331.         long    PRIM_type;        /* Always T_MIB_ACK     */
  332.         long    MIB_length;        /* MIB length             */        
  333.         long    MIB_offset;        /* MIB Offset            */        
  334.     };
  335.     
  336.     struct T_stream_timer
  337.     {
  338.         long        PRIM_type;    /* Always T_TIMER_REQ */
  339.         union
  340.         {
  341.             long    USER_long;
  342.             void*    USER_ptr;
  343.     #ifndef __cplusplus
  344.         }            USER_data;
  345.     #else
  346.         };
  347.     #endif
  348.     };
  349.  
  350. /*    -------------------------------------------------------------------------
  351.     Internal port record structures
  352.     ------------------------------------------------------------------------- */
  353.  
  354.     /*    
  355.      * One TPortRecord is created for each instance of a port.
  356.      * For Instance 'enet' identifies an ethernet port.
  357.      * An TPortRecord for each ethernet card it finds, with an
  358.      * OTPortRef that will uniquely allow the ethernet driver to determine which
  359.      * port it is supposed to open on.
  360.      */
  361.  
  362.     typedef struct TPortRecord    TPortRecord;
  363.  
  364.     struct TPortRecord
  365.     {
  366.         OTLink                    fLink;
  367.         char*                    fPortName;
  368.         char*                    fModuleName;
  369.         char*                    fResourceInfo;
  370.         char*                    fSlotID;
  371.         struct TPortRecord*        fAlias;
  372.         size_t                    fNumChildren;
  373.         OTPortRef*                fChildPorts;
  374.         UInt32                    fPortFlags;
  375.         UInt32                    fInfoFlags;
  376.         UInt32                    fCapabilities;
  377.         OTPortRef                fRef;
  378.         struct streamtab*        fStreamtab;
  379.         void*                    fContext;
  380.         void*                    fExtra;
  381.     };
  382.  
  383. /*******************************************************************************
  384. ** "C" Interface routines
  385. ********************************************************************************/
  386.  
  387. #ifdef __cplusplus
  388. extern "C" {
  389. #endif
  390.  
  391. /*    -------------------------------------------------------------------------
  392.     Interrupt control functions
  393.     ------------------------------------------------------------------------- */
  394.  
  395. #if USES68KINLINES
  396.     //
  397.     // MPS_INTR_STATE saves the current interrupt state
  398.     // Its definition undoubtably changes with the definition
  399.     // of mps intr enable/disable, so this typedef is
  400.     // also inside the USE68KINLINES conditional.
  401.     //
  402.     typedef UInt8    MPS_INTR_STATE;
  403.     //
  404.     // Disable interrupts and save the state
  405.     //
  406.     #pragma parameter mps_intr_disable(__A0)
  407.     void mps_intr_disable(MPS_INTR_STATE*) =
  408.     {
  409.         0x40C0,             /*        move    sr,d0        */
  410.         0xE040,                /*        asr.w    #8,d0        */
  411.         0x007C, 0x0600,        /*        ori        #$600,sr    */
  412.         0x027C, 0xFEFF,     /*        andi    #$FEFF,sr    */
  413.         0x1080                /*         move.b d0,(a0)         */
  414.     #if qDebug
  415.         , 0x0200, 0x0070,    /*        andi.b    #$70,d0        */
  416.         0x0c00, 0x0070,        /*        cmpi.b    #$70,d0        */
  417.         0x6602,                /*        bne.s    @3            */
  418.         0xA9FF                /*        DebugBreak            */
  419.     #endif
  420.     };
  421.     //
  422.     // Enable interrupts from the saved state
  423.     //
  424.     #pragma parameter mps_intr_enable(__A0)
  425.     void mps_intr_enable(MPS_INTR_STATE* ptr) =
  426.     {
  427.     #if qDebug
  428.         0x40C0,             /*        move    sr,d0        */
  429.         0x0240, 0x0700,        /*        andi.w    #$700,d0    */
  430.         0x0C40, 0x0600,        /*        cmpi.w    #$600,d0    */
  431.         0x6702,                /*         beq.s    @2             */
  432.         0xA9FF,                /*         DebugBreak             */
  433.     #endif
  434.         0x1010,                /* @2    move.b    (a0),d0        */
  435.     #if qDebug
  436.         0x0200, 0x0070,        /*        andi.b    #$70,d0        */
  437.         0x0c00, 0x0070,        /*        cmpi.b    #$70,d0        */
  438.         0x6602,                /*        bne.s    @3            */
  439.         0xA9FF,                /*        DebugBreak            */
  440.         0x1010,                /* @3    move.b    (a0),d0        */
  441.     #endif
  442.         0xE140,                /*        asl.w    #8,d0        */
  443.         0x46C0                /*        move    d0,sr         */
  444.     };
  445. #else
  446.     
  447.         typedef UInt8    MPS_INTR_STATE;
  448.  
  449.         void mps_intr_disable(MPS_INTR_STATE*);
  450.         void mps_intr_enable(MPS_INTR_STATE*);
  451.  
  452. #endif
  453.  
  454. /*    -------------------------------------------------------------------------
  455.     The kOTPortScannerInterfaceID define is what you need to add to your
  456.     export file for the "interfaceID = " clause.  
  457.     PortScanProcPtr is the typedef for the scanning function.
  458.     Your port-scanning function must be exported by the name "OTScanPorts".
  459.     Your port-scanning function set must use the prefix kOTPortScannerPrefix.
  460.     ------------------------------------------------------------------------- */
  461.  
  462.     #define kOTPortScannerInterfaceID            kOTKernelPrefix "pScnr"
  463.     #define kOTPseudoPortScannerInterfaceID        kOTKernelPrefix "ppScnr"
  464.     
  465. #ifdef __cplusplus
  466. extern "C" {
  467. #endif
  468.  
  469.     typedef void (*PortScanProcPtr)(UInt32 scanType);
  470.  
  471. #ifdef __cplusplus
  472. }
  473. #endif
  474.  
  475.     enum    /* scanType */
  476.     {
  477.         kOTInitialScan            = 0,
  478.         kOTScanAfterSleep        = 1
  479.     };
  480.             
  481. /*    -------------------------------------------------------------------------
  482.     Port Registration
  483.     
  484.     These routines can be used to register, find and iterate through the
  485.     various ports on the machine
  486.     ------------------------------------------------------------------------- */
  487.         //
  488.         // Register a port. The name the port was registered under is returned in
  489.         // the fPortName field.
  490.         //
  491.     extern OSStatus    OTRegisterPort(OTPortRecord* portInfo, void* ref);
  492.         //
  493.         // Unregister the port with the given name (If you re-register the
  494.         // port, it may get a different name - use OTChangePortState if
  495.         // that is not desireable).  Since a single OTPortRef can be registered
  496.         // with several names, the API needs to use the portName rather than
  497.         // the OTPortRef to disambiguate.
  498.         //
  499.     extern OSStatus    OTUnregisterPort(const char* portName, void**);
  500.         //
  501.         // Change the state of the port.
  502.         //
  503.     extern OSStatus    OTChangePortState(OTPortRef, OTEventCode theChange, OTResult why);
  504.         //
  505.         // Find the TPortRecord for a given Port Name
  506.         //
  507.     TPortRecord*    OTFindPort(const char* portName);
  508.         //
  509.         // Find the "nth" TPortRecord
  510.         //
  511.     TPortRecord*    OTGetIndexedPort(size_t index);
  512.         //
  513.         // Find another port that is active and conflicts with
  514.         // the port described by "ref"
  515.         //
  516.     TPortRecord*    OTFindPortConflict(OTPortRef ref);
  517.         //
  518.         // Other ways of finding the port
  519.         //
  520.     TPortRecord*    OTFindPortByRef(OTPortRef);
  521.     TPortRecord*    OTFindPortByDev(dev_t);
  522.         //
  523.         // Memory allocation for OTRegisterPort persistent data
  524.         //
  525.     void*            OTAllocPortMem(size_t);
  526.     void            OTFreePortMem(void*);
  527.     
  528. /*    -------------------------------------------------------------------------
  529.     Timer functions
  530.     ------------------------------------------------------------------------- */
  531.  
  532.     enum
  533.     {
  534.         kOTMinimumTimerValue    = 8        // 8 milliseconds is the minimum timeout value
  535.     };
  536.     
  537.     mblk_t*            mi_timer_q_switch(mblk_t*, queue_t*, mblk_t*);
  538.     Boolean            mi_timer_cancel(mblk_t*);
  539.     void            mi_timer(mblk_t*, unsigned long);
  540.     mblk_t*            mi_timer_alloc(queue_t*, size_t);
  541.     void            mi_timer_free(mblk_t*);
  542.     Boolean            mi_timer_valid(mblk_t*);
  543.  
  544. /*    -------------------------------------------------------------------------
  545.     Miscellaneous helpful routines
  546.     ------------------------------------------------------------------------- */
  547.     /*
  548.      * This routine is used by a driver at interrupt time to schedule
  549.      * a deferred task or SWI to run their interrupt processing code.
  550.      */
  551.      Boolean OTScheduleDriverDeferredTask(long dtCookie);
  552.     /*
  553.      * This is the typedef for a function that will be called when a message
  554.      * created by OTAllocMsg is destroyed.
  555.      */
  556. #ifdef __cplusplus
  557. extern "C" {
  558. #endif
  559.  
  560.     typedef void    (*EsbFreeProcPtr)(char* arg);
  561.     
  562. #ifdef __cplusplus
  563. }
  564. #endif
  565.     /*
  566.      * This function creates a message which points to "size" bytes of data
  567.      * at "buf".  When the message is freed, the EsbFreeProcPtr function "func"
  568.      * will be called with the argument "arg".
  569.      * NOTE: This function allows users of your buffer to modify the buffer.
  570.      */
  571.     mblk_t* OTAllocMsg(void* buf, size_t size,  EsbFreeProcPtr func, void* arg);
  572.  
  573.     /*
  574.      * Routines to allocate and free memory in your modules (these are
  575.      * interrupt-time safe!). 
  576.      */
  577.  
  578.     void*    OTAllocMem(size_t);
  579.     void    OTFreeMem(void*);
  580.     void*    OTReallocMem(void* ptr, size_t newSize);
  581.  
  582.     /*
  583.      * Routines to calculate various sizes of STREAM messages
  584.      */
  585.     #define HEAD_SIZE(mp)    ((mp)->b_rptr - (mp)->b_datap->db_base)
  586.     #define TAIL_SIZE(mp)    ((mp)->b_datap->db_lim - (mp)->b_wptr)
  587.     #define MBLK_SIZE(mp)    ((mp)->b_wptr - (mp)->b_rptr)
  588.     #define DBLK_SIZE(mp)    ((mp)->b_datap->db_lim - (mp)->b_datap->db_base)
  589.     
  590.     #ifdef __cplusplus
  591.     extern "C" {
  592.     #endif
  593.     
  594.     int        mi_bcmp(const char* first, const char* second, size_t nBytes);
  595.     int        mi_sprintf (char * buf, char * fmt, ...);
  596.     
  597.     #ifdef __cplusplus
  598.     }
  599.     #endif
  600.     
  601.     #define bcopy(s, d, l)            OTMemcpy(d, s, l)
  602.     #define bzero(d, l)                OTMemzero(d, (size_t)(l))
  603.     #define bcmp(s, d, l)            mi_bcmp(s, d, l)
  604.  
  605.     /*
  606.      * Standard STREAMS bcopy, bzero, & bcmp take char* parameters.
  607.      * The BCOPY, BZERO, and BCMP routines take void* so that we do not have
  608.      * to cast all the pointers.
  609.      */
  610.     #define BCOPY(s, d, l)            bcopy((const char*)(s), (char*)(d), l)
  611.     #define BZERO(d, l)                bzero((char*)(d), l)
  612.     #define BCMP(s, d, l)            mi_bcmp((const char*)(s), (const char*)(d), l)
  613.  
  614.     /*
  615.      * Create sprintf and printf functions that will work in STREAM modules.
  616.      * Also, make sure that calling traditional "C" allocation routines
  617.      * will not compile.
  618.      */
  619.     #define sprintf                        mi_sprintf
  620.     #define printf                        OTKernelPrintf
  621.  
  622.     #define calloc                        (DONT_CALL*THIS_FUNCTION)
  623.     #define malloc                        (DONT_CALL*THIS_FUNCTION)
  624.     #define realloc                        (DONT_CALL*THIS_FUNCTION)
  625.     #define free                        (DONT_CALL*THIS_FUNCTION)
  626.  
  627.     /*
  628.      * A few other miscellaneous functions
  629.      */
  630.     int            drv_priv(struct cred* credp);
  631.     queue_t*    mi_allocq(struct streamtab*);
  632.     mblk_t*        mi_reallocb(mblk_t*, size_t new_Size);
  633.     
  634. #ifdef __cplusplus
  635. }
  636. #endif
  637.  
  638. /*******************************************************************************
  639. ** Some helpful utilites from Mentat
  640. ********************************************************************************/
  641.  
  642. #define    MI_COPY_IN        1
  643. #define    MI_COPY_OUT        2
  644. #define    MI_COPY_DIRECTION(mp)                             \
  645.     (((UInt8*)&(mp)->b_cont->b_prev)[0])
  646. #define    MI_COPY_COUNT(mp)                     \
  647.     (((UInt8*)&(mp)->b_cont->b_prev)[1])
  648. #define    MI_COPY_RVAL(mp)    (*(int *)&(mp)->b_cont->b_next)
  649. #define    MI_COPY_CASE(dir,cnt)    ((int)(((cnt)<<2)|dir))
  650. #define    MI_COPY_STATE(mp)                             \
  651.     ((int)MI_COPY_CASE(MI_COPY_DIRECTION(mp),MI_COPY_COUNT(mp)))
  652.  
  653. #ifdef __cplusplus
  654. extern "C" {
  655. #endif
  656.  
  657. typedef void    (*OTWriterProcPtr)(queue_t*, mblk_t*);
  658.  
  659. void    mps_become_writer(queue_t* q, mblk_t* mp, OTWriterProcPtr proc);
  660.  
  661. mblk_t*    mi_tpi_ack_alloc(mblk_t* mp, size_t size, long);
  662. mblk_t*    mi_tpi_conn_con(mblk_t* trailer_mp, char* src, size_t src_length, char* opt, size_t opt_length);
  663. mblk_t*    mi_tpi_conn_ind(mblk_t* trailer_mp, char* src, size_t src_length, char* opt, size_t opt_length, int seqnum);
  664. mblk_t*    mi_tpi_conn_req(mblk_t* trailer_mp, char* dest, size_t dest_length, char* opt, size_t opt_length);
  665. mblk_t*    mi_tpi_data_ind(mblk_t* trailer_mp, int flags, long type);
  666. mblk_t*    mi_tpi_data_req(mblk_t* trailer_mp, int flags, long type);
  667. mblk_t*    mi_tpi_discon_ind(mblk_t* trailer_mp, int reason, int seqnum);
  668. mblk_t*    mi_tpi_discon_req(mblk_t* trailer_mp, int seqnum);
  669. mblk_t*    mi_tpi_err_ack_alloc(mblk_t* mp, int tlierr, int unixerr);
  670. mblk_t*    mi_tpi_exdata_ind(mblk_t* trailer_mp, int flags, long type);
  671. mblk_t*    mi_tpi_exdata_req(mblk_t* trailer_mp, int flags, long type);
  672. mblk_t*    mi_tpi_info_req(void);
  673. mblk_t*    mi_tpi_ok_ack_alloc(mblk_t* mp);
  674. mblk_t*    mi_tpi_ordrel_ind(void);
  675. mblk_t*    mi_tpi_ordrel_req(void);
  676. mblk_t*    mi_tpi_uderror_ind(char* dest, size_t dest_length, char* opt, size_t opt_length, int error);
  677. mblk_t*    mi_tpi_unitdata_ind(mblk_t* trailer_mp, char* src, size_t src_length, char* opt, size_t opt_length);
  678. mblk_t*    mi_tpi_unitdata_req(mblk_t* trailer_mp, char* dst, size_t dst_length, char* opt, size_t opt_length);
  679. mblk_t* mi_reuse_proto(mblk_t* toReuse, size_t sizeDesired, boolean_p keepOnError);
  680.  
  681. mblk_t* mi_reallocb(mblk_t* old_mp, size_t new_size);
  682.  
  683. Boolean    mi_set_sth_hiwat(queue_t* q, size_t size);
  684. Boolean    mi_set_sth_lowat(queue_t* q, size_t size);
  685. Boolean    mi_set_sth_maxblk(queue_t* q, size_t size);
  686. Boolean    mi_set_sth_wroff(queue_t* q, size_t size);
  687.  
  688. UInt8* mi_offset_param(mblk_t* mp, long offset, long len);
  689. UInt8* mi_offset_paramc(mblk_t* mp, long offset, long len);
  690.  
  691. char* mi_open_detached(char** mi_opp_orig, size_t size, dev_t* devp);
  692. int mi_open_comm(char** mi_opp_orig, size_t size, queue_t* q, dev_t* dev,
  693.                  int flag, int sflag, cred_t* credp);
  694. int mi_close_comm(char** mi_opp_orig, queue_t* q);
  695. void mi_bufcall(queue_t* q, size_t size, int pri);
  696. void mi_detach(queue_t* q, char* ptr);
  697. void mi_close_detached(char** mi_opp_orig, char* ptr);
  698. char* mi_next_ptr(char*);
  699.  
  700. void mi_copyin(queue_t* q, mblk_t* mp, char* uaddr, size_t len);
  701. void mi_copyout(queue_t* q, mblk_t* mp);
  702. mblk_t* mi_copyout_alloc(queue_t* q, mblk_t* mp, char* uaddr, size_t len);
  703. void mi_copy_done(queue_t* q, mblk_t* mp, int err);
  704. void mi_copy_set_rval(mblk_t* mp, int rval);
  705. int mi_copy_state(queue_t* q, mblk_t* mp, mblk_t** mpp);
  706.  
  707. #ifdef __cplusplus
  708. }
  709.  
  710. /*******************************************************************************
  711. ** Everything below here is C++ ONLY
  712. ********************************************************************************/
  713.  
  714. /*    -------------------------------------------------------------------------
  715.     TStreamQueue class
  716.     
  717.     This class is just a convenient interface to the queue structure
  718.     ------------------------------------------------------------------------- */
  719.  
  720.     class TStreamQueue : public queue
  721.     {
  722.         public:
  723.             void        EnableQueue();
  724.     };
  725.     
  726.     /*    -------------------------------------------------------------------------
  727.         Inline methods for TStreamQueue
  728.         ------------------------------------------------------------------------- */
  729.     
  730.         inline void TStreamQueue::EnableQueue()
  731.         {
  732.             if ( q_flag & QNOENB )
  733.             {
  734.                 enableok(this);
  735.                 if ( q_first )
  736.                     qenable(this);
  737.             }
  738.         }
  739.  
  740. /*    -------------------------------------------------------------------------
  741.     Class TStreamMessage
  742.     
  743.     This class is a C++ interface to the mblk_t structure defined in STREAMS
  744.     ------------------------------------------------------------------------- */
  745.     //
  746.     // The maximum stream buffer size is the largest "size_t" that
  747.     // doesn't look negative if interpreted as a signed number
  748.     //
  749.     enum
  750.     {
  751.         kMaxStreamBufferSize    = (((size_t)-1L) >> 1)
  752.     };
  753.  
  754.     class TStreamMessage : public msgb
  755.     {
  756.         private:
  757.                 void*        operator new (size_t)    { return NULL; }
  758.                 
  759.         public:
  760.                 void*        operator new(size_t, size_t size)
  761.                     { return (TStreamMessage*)allocb(size, 0); }
  762.                 void*        operator new(size_t, void* buf, size_t size,
  763.                                          EsbFreeProcPtr func, void* arg)
  764.                     { return OTAllocMsg(buf, size, func, arg); }
  765.                 void        operator delete(void* ptr)
  766.                     { if ( ptr != NULL ) freemsg((mblk_t*)ptr); }
  767.     
  768.                     void            Reset(size_t = 0);
  769.                     void            ResetWithLeader(size_t);
  770.             
  771.                     void            FreeData();
  772.                     TStreamMessage*    RemoveData();
  773.                     void            AppendData(TStreamMessage* data);
  774.             
  775.                     size_t            GetSize() const;
  776.                     size_t            GetDataSize() const;
  777.                     size_t            GetMessageDataSize() const;
  778.                     void            SetDataSize(size_t);
  779.         
  780.                     void            SetType(UInt8);
  781.                     UInt8            GetType() const;
  782.                     TStreamMessage*    GetNextBlock() const;
  783.                     TStreamMessage*    ReuseMessage(size_t newSize, boolean_p keepOnFailure);
  784.                     
  785.                     void            SetNextBlock(TStreamMessage*);
  786.                     char*            GetDataPointer() const;
  787.                     Boolean            IsReuseable(size_t size) const;
  788.             
  789.                     void            HideBytesAtFront(size_t);
  790.                     void            HideBytesAtEnd(size_t);
  791.             //
  792.             // The following functions ignore non-M_DATA blocks.
  793.             //
  794.                     const TStreamMessage*
  795.                                     _MDECL GetBlockAt(size_t& offset) const;
  796.                     const void*        _MDECL GetPointerTo(size_t offset, size_t* len) const;
  797.                     const void*        _MDECL GetPointerTo(size_t offset, size_t* len, void* bfr) const;
  798.             //
  799.             // This function makes a non-shared copy of any initial non-M_DATA block, and
  800.             // then makes shared copies of the requested data.  It only assumes 1 leading
  801.             // non-M_DATA block, but will skip any other non-M_DATA blocks while 
  802.             // searching for data to copy.
  803.             //
  804.                     TStreamMessage*    _MDECL MakeSharedCopy(size_t offset = 0, 
  805.                                                           size_t len = kMaxStreamBufferSize);
  806.  
  807.                     Boolean            _MDECL HasData() const;
  808.                     Boolean            _MDECL WriteData(void* buf, size_t len);
  809.             //
  810.             // These hide/remove the part of the data already read.  
  811.             //
  812.                     TStreamMessage*    _MDECL ReadData(void* buf, size_t* len);
  813.                     size_t            _MDECL ReadControl(void* buf, size_t len);
  814.     };
  815.     
  816.     /*    -------------------------------------------------------------------------
  817.         Inline methods for TStreamMessage
  818.         ------------------------------------------------------------------------- */
  819.     
  820.         inline void TStreamMessage::Reset(size_t size)
  821.         {
  822.             b_rptr = b_datap->db_base;
  823.             b_wptr = b_rptr + size;
  824.         }
  825.         
  826.         inline size_t OTLengthWithLeader(size_t size)
  827.         {
  828.             return (size + 3) & ~3;
  829.         }
  830.         
  831.         inline void TStreamMessage::ResetWithLeader(size_t size)
  832.         {
  833.             b_rptr = b_datap->db_lim - OTLengthWithLeader(size);
  834.             b_wptr = b_rptr + size;
  835.         }
  836.         
  837.         inline void TStreamMessage::FreeData()
  838.         {
  839.             if ( b_cont )
  840.             {
  841.                 freemsg(b_cont);
  842.                 b_cont = NULL;
  843.             }
  844.         }
  845.         
  846.         inline TStreamMessage* TStreamMessage::RemoveData()
  847.         {
  848.             TStreamMessage* temp = (TStreamMessage*)b_cont;
  849.             b_cont = NULL;
  850.             return temp;
  851.         }
  852.         
  853.         inline void TStreamMessage::AppendData(TStreamMessage* mp)
  854.         {
  855.             linkb(this, mp);
  856.         }
  857.     
  858.         inline size_t TStreamMessage::GetSize() const
  859.         {
  860.             return b_datap->db_lim - b_datap->db_base;
  861.         }
  862.         
  863.         inline size_t TStreamMessage::GetDataSize() const
  864.         {
  865.             return b_wptr - b_rptr;
  866.         }
  867.         
  868.         inline size_t TStreamMessage::GetMessageDataSize() const
  869.         {
  870.             return (b_cont == NULL) ? 
  871.                 (b_datap->db_type == M_DATA ? (b_wptr - b_rptr) : 0) : msgdsize(this);
  872.         }
  873.     
  874.         inline void TStreamMessage::SetDataSize(size_t size)
  875.         {
  876.             b_wptr = b_rptr + size;
  877.         }
  878.     
  879.         inline void TStreamMessage::SetType(UInt8 type)
  880.         {
  881.             b_datap->db_type = type;
  882.         }
  883.     
  884.         inline unsigned char TStreamMessage::GetType() const
  885.         {
  886.             return b_datap->db_type;
  887.         }
  888.         
  889.         inline TStreamMessage* TStreamMessage::GetNextBlock() const
  890.         {
  891.             return (TStreamMessage*)b_cont;
  892.         }
  893.         
  894.         inline void TStreamMessage::SetNextBlock(TStreamMessage* mp)
  895.         {
  896.             b_cont = (mblk_t*)mp;
  897.         }
  898.         
  899.         inline char* TStreamMessage::GetDataPointer() const
  900.         {
  901.             return (char*)b_rptr;
  902.         }
  903.         
  904.         inline Boolean TStreamMessage::IsReuseable(size_t size) const
  905.         {
  906.             return (b_datap->db_ref == 1 && GetSize() >= size);
  907.         }
  908.         
  909.         inline void TStreamMessage::HideBytesAtFront(size_t len)
  910.         {
  911.             adjmsg(this, len);
  912.         }
  913.         
  914.         inline void TStreamMessage::HideBytesAtEnd(size_t len)
  915.         {
  916.             adjmsg(this, -len);
  917.         }
  918.  
  919.         inline TStreamMessage* TStreamMessage::ReuseMessage(size_t newSize, boolean_p keepOnFailure)
  920.         {
  921.             return (TStreamMessage*)mi_reuse_proto(this, newSize, keepOnFailure);
  922.         }
  923.         
  924. /*    -------------------------------------------------------------------------
  925.     TTimerMessage class
  926.     
  927.     This class implements an interface to the STREAM environment timer
  928.     facilities.  A TTimerMessage will be placed on the queue of your choice
  929.     when it's timer expires.
  930.     ------------------------------------------------------------------------- */
  931.  
  932. #if GENERATINGPOWERPC
  933.     #define TIMER_BUG    1
  934. #else
  935.     #define TIMER_BUG    0
  936. #endif
  937.  
  938.     class TTimerMessage : public TStreamMessage
  939.     {
  940.     #if TIMER_BUG
  941.         private:
  942.     #else
  943.         public:
  944.     #endif
  945.             inline void* operator new(size_t, queue_t* q)
  946.                 {    return mi_timer_alloc(q, sizeof(T_stream_timer)); }
  947.             inline void* operator new(size_t, queue_t* q, size_t extra)
  948.                 {    return mi_timer_alloc(q, extra + sizeof(T_stream_timer)); }
  949.     
  950.             inline void operator delete(void* ptr)
  951.                 {    if ( ptr != NULL ) mi_timer_free((mblk_t*)ptr); }
  952.     
  953.         public:        
  954.                     Boolean            IsValid();
  955.                     TTimerMessage*    ChangeQueue(TStreamQueue* newQ, TTimerMessage* newMP);
  956.                     Boolean            Cancel();
  957.                     void            Schedule(OTTimeout time);
  958.     
  959.         private:
  960.             inline void* operator new(size_t)
  961.                 {    return 0; }
  962.     };
  963.     
  964.     inline TTimerMessage* NewTimerMsg(queue_t* q, size_t extra = 0)
  965.     {
  966.         return (TTimerMessage*)mi_timer_alloc(q, sizeof(T_stream_timer) + extra);
  967.     }
  968.     
  969.     inline void FreeTimerMsg(TTimerMessage* msg)
  970.     {
  971.         mi_timer_free(msg);
  972.     }
  973.     
  974.     /*    -------------------------------------------------------------------------
  975.         Inline methods for TStreamTimer
  976.         ------------------------------------------------------------------------- */
  977.     
  978.         inline Boolean TTimerMessage::IsValid()
  979.         {
  980.             return mi_timer_valid(this);
  981.         }
  982.         
  983.         inline TTimerMessage* TTimerMessage::ChangeQueue(TStreamQueue* q, TTimerMessage* newMP)
  984.         {
  985.             return( (TTimerMessage*) mi_timer_q_switch(this, q, newMP));
  986.         }
  987.     
  988.         inline Boolean TTimerMessage::Cancel()
  989.         {
  990.             return mi_timer_cancel(this);
  991.         }
  992.         
  993.         inline void TTimerMessage::Schedule(OTTimeout time)
  994.         {
  995.             ((T_stream_timer*)b_rptr)->PRIM_type = T_TIMER_REQ;
  996.             mi_timer(this, (unsigned long)time);
  997.         }
  998.  
  999. #endif    /* __cplusplus    */
  1000.  
  1001. #if defined(__MWERKS__) && GENERATING68K
  1002. #pragma pointers_in_A0
  1003. #endif
  1004.  
  1005. #if PRAGMA_ALIGN_SUPPORTED
  1006. #pragma options align=reset
  1007. #endif
  1008.     
  1009. #endif    /* __OPENTPTMODULE__    */
  1010.